package com.mitcub.panda.controller;

import com.mitcub.panda.base.BaseController;
import com.mitcub.panda.entity.Role;
import com.mitcub.panda.query.RoleQuery;
import com.mitcub.panda.service.IRoleService;
import com.mitcub.panda.utils.RespResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @项目名称: panda
 * @类名称: SystemRoleController
 * @类描述:
 * @作者: tangloong
 * @创建时间: 2021/11/9 15:51
 */
@Controller
@RequestMapping("/system/role")
public class SystemRoleController extends BaseController {

    @Autowired
    private IRoleService roleService;

    /**
     * 获取数据列表
     * @param query 查询条件
     * @return
     */
    @ResponseBody
    @RequestMapping("/list")
    public RespResult list(RoleQuery query) {
        return roleService.getList(query);
    }

    /**
     * 添加记录
     * @param entity 实体对象
     * @return
     */
    @ResponseBody
    @PostMapping("/add")
    public RespResult add(@RequestBody Role entity) {
        return roleService.edit(entity);
    }

    /**
     * 修改记录
     * @param entity 实体对象
     * @return
     */
    @ResponseBody
    @PostMapping("/update")
    public RespResult update(@RequestBody Role entity) {
        return roleService.edit(entity);
    }

    /**
     * 获取记录详情
     * @param id    记录ID
     * @param model 模型
     * @return
     */
    @Override
    public String edit(Long id, Model model) {
        Map<String, Object> info = new HashMap<>();
        if (id != null && id > 0) {
            info = roleService.info(id);
        }
        model.addAttribute("info", info);
        return super.edit(id, model);
    }

    /**
     * 删除记录
     * @param id 记录ID
     * @return
     */
    @ResponseBody
    @GetMapping("/delete/{id}")
    public RespResult delete(@PathVariable("id") Long id) {
        return roleService.deleteById(id);
    }

    /**
     * 批量删除
     * @param ids 记录ID(多个使用逗号","分隔)
     * @return
     */
    @ResponseBody
    @GetMapping("/batchDelete/{ids}")
    public RespResult batchDelete(@PathVariable("ids") String ids) {
        return roleService.deleteByIds(ids);
    }

    /**
     * 根据ID 获取权限列表
     * @param id
     * @return
     */
    @ResponseBody
    @GetMapping("/getRolePermissionByRoleId")
    public RespResult getRolePermissionByRoleId(Integer id) {
        return roleService.getRolePermissionByRoleId(id);
    }

    /**
     * 设置 角色的权限
     * @param role
     * @return
     */
    @ResponseBody
    @PostMapping("/setRolePermission")
    public RespResult setRolePermission(@RequestBody Role role){
        if (role.getId() == null || role.getId() <= 0) {
            return RespResult.error("ID不能为空");
        }
        if (role.getAuth() == null) {
            return RespResult.error("权重不能为空");
        }
        Role entity = new Role();
        entity.setId(role.getId());
        entity.setAuth(role.getAuth());
        return roleService.setRolePermission(entity);
    }

    /**
     * 设置状态
     * @param entity 实体对象
     * @return
     */
    @ResponseBody
    @PostMapping("/setStatus")
    public RespResult setStatus(@RequestBody Role entity) {
        if(entity.getId() == 1){
            return RespResult.error("超级管理员禁止设置状态");
        }
        Role role = new Role() ;
        role.setId(entity.getId());
        role.setStatus(entity.getStatus());
        return roleService.setStatus(role);
    }

}
